package com.wss.common.widget;

import com.wss.common.utils.PxUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class ViewPagerForScrollView extends PageSlider implements
        Serializable,
        Component.EstimateSizeListener,
        Component.TouchEventListener {

    private static final long serialVersionUID = 1L;


    private int current;
    private int height = 0;
    /**
     * 保存position与对于的View
     */
    private HashMap<Integer, Component> mChildrenViews = new LinkedHashMap<>();
    private boolean scroll = true;
    /**
     * 最小高度
     */
    private int minHeight;

    public ViewPagerForScrollView(Context context) {
        this(context, null);
    }

    public ViewPagerForScrollView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public ViewPagerForScrollView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        minHeight = PxUtils.getScreenHeight(context);
        setEstimateSizeListener(this);
        setTouchEventListener(this);
    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {

        if (mChildrenViews.size() > current) {
            Component child = mChildrenViews.get(current);
            if (child != null) {
                child.estimateSize(widthMeasureSpec, heightMeasureSpec);
                height = child.getEstimatedHeight();
                if (height < minHeight) {
                    height = minHeight;
                }
            }
        }

        heightMeasureSpec = EstimateSpec.getSizeWithMode(height,
                EstimateSpec.getMode(heightMeasureSpec));

        setEstimatedSize(widthMeasureSpec, heightMeasureSpec);

        return true;
    }

    /**
     * 重置ViewPager的高度
     *
     * @param current 第几页的
     */
    public void resetHeight(int current) {
        this.current = current;
        if (mChildrenViews.size() > current) {
            ComponentContainer.LayoutConfig layoutParams = getLayoutConfig();
            if (layoutParams == null) {
                layoutParams = new ComponentContainer
                        .LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, height);
            } else {
                layoutParams.height = height;
            }

            setLayoutConfig(layoutParams);
        }
    }

    /**
     * 保存position与对于的View
     */
    public void setObjectForPosition(Component view, int position) {
        mChildrenViews.put(position, view);
    }


    /**
     * 设置ViewPager显示的最小高度
     *
     * @param minHeight 最小高度
     */
    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    /**
     * 设置是否可滚动
     *
     * @param scroll 滚动
     */
    public void setScroll(boolean scroll) {
        this.scroll = scroll;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        if (!scroll) {
            return true;
        }
        return false;
    }
}
